package com.wg.dome.handle;

import com.wg.dome.ClientPeer;
import com.wg.dome.ErrorCode;
import com.wg.dome.ErrorHelper;
import com.wg.dome.annotation.Request;
import com.wg.dome.attribute.Attribute;
import com.wg.dome.attribute.AttributeHelper;
import com.wg.dome.chat.ChatConstant;
import com.wg.dome.chat.ChatLobby;
import com.wg.dome.entity.config.InitDataEntity;
import com.wg.dome.entity.user.PlayerEntity;
import com.wg.dome.operation.GetPlayerInfoRequest;
import com.wg.dome.operation.GetPlayerInfoResponse;
import com.wg.dome.operation.OperationCode;
import com.wg.dome.repository.*;
import com.wg.common.net.OperationRequest;
import com.wg.common.net.OperationResponse;
import com.wg.common.net.SendParameters;
import com.wg.common.lang.HashTable;

/**
 * 获取玩家基本信息
 */
@Request(OperationCode = OperationCode.GET_PLAYER_INFO)
public class HandleGetBaseInfoRequest extends BaseHandle {

    @Override
    public OperationResponse execute(ClientPeer peer, OperationRequest request, SendParameters sendParameters) {
        GetPlayerInfoRequest getPlayerInfoRequest = new GetPlayerInfoRequest(request);
        if (!getPlayerInfoRequest.isValid()) {
            return ErrorHelper.getInvalidResponse(getPlayerInfoRequest);
        }

        if (getPlayerInfoRequest.getPlayerId() != peer.getPlayerId()) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.PLAYERID_MATCH_ERROR, getPlayerInfoRequest.getPlayerId(), peer.getPlayerId());
        }

        ChatLobby chatLobby = peer.getApplication().getChatLobby();
        chatLobby.onAddChannel(ChatConstant.CHANNEL_PUBLIC, peer);

        PlayerRepository playerRepository = new PlayerRepository();
        PlayerEntity playerEntity = playerRepository.get(peer.getPlayerId());
        if (playerEntity == null) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(), ErrorCode.PLAYER_DATA_ERROR, peer.getPlayerId());
        }

        Attribute[] attributes = AttributeHelper.getFightAttributes(peer.getPlayerId());
        HashTable[] attributeTables = new HashTable[attributes.length];
        for (int i = 0; i < attributeTables.length; i++) {
            attributeTables[i] = attributes[i].toHashTable();
        }

        InitDataRepository initDataRepository = new InitDataRepository();
        InitDataEntity initDataEntity = initDataRepository.get();

        GetPlayerInfoResponse getPlayerInfoResponse = new GetPlayerInfoResponse();
        getPlayerInfoResponse.setPlayerName(playerEntity.getName());
        getPlayerInfoResponse.setPlayerPower(playerEntity.getXw());
        getPlayerInfoResponse.setPlayerGold(playerEntity.getGold());
        getPlayerInfoResponse.setPlayerMoney(playerEntity.getRmb());
        getPlayerInfoResponse.setFighAttribute(attributeTables);
        getPlayerInfoResponse.setSurplus(playerEntity.getSurplus());

        if (playerEntity.getRecruitTime() == 0) {
            getPlayerInfoResponse.setFreeRecruitTime(0);
        } else {
            getPlayerInfoResponse.setFreeRecruitTime(initDataEntity.getFreeRecruitIntervalTime() - (System.currentTimeMillis() / 1000 - playerEntity.getRecruitTime()));
        }

        return new OperationResponse(request.getOperationCode(), getPlayerInfoResponse);
    }
}
